home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / ActView / ActiveViewer.jar / com / simeda / ActiveViewer / VNCController.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-12-12  |  4.4 KB  |  147 lines

  1. package com.simeda.ActiveViewer;
  2.  
  3. import java.io.IOException;
  4. import javax.microedition.lcdui.Display;
  5. import javax.microedition.lcdui.Displayable;
  6.  
  7. public class VNCController implements Runnable {
  8.    vncCanvas displayable = null;
  9.    private String host = null;
  10.    private String password = null;
  11.    private int port = -1;
  12.    public rfbProto rfb = null;
  13.    public Display display = null;
  14.    private Displayable next = null;
  15.  
  16.    public VNCController(Displayable var1) {
  17.       this.next = var1;
  18.    }
  19.  
  20.    public void setHost(String var1) {
  21.       this.host = var1;
  22.    }
  23.  
  24.    public void setPort(int var1) {
  25.       this.port = var1;
  26.    }
  27.  
  28.    public void setPassword(String var1) {
  29.       this.password = var1;
  30.    }
  31.  
  32.    public void setDisplay(Display var1) {
  33.       this.display = var1;
  34.    }
  35.  
  36.    public void doConnect() {
  37.       try {
  38.          this.displayable = new vncCanvas(this);
  39.          this.display.setCurrent(this.displayable);
  40.          this.connectAndAuthenticate();
  41.          this.doProtocolInitialisation();
  42.          Thread var1 = new Thread(this);
  43.          var1.start();
  44.       } catch (Exception var2) {
  45.          ((Throwable)var2).printStackTrace();
  46.       }
  47.  
  48.    }
  49.  
  50.    public void run() {
  51.       try {
  52.          this.displayable.doInit(this.rfb);
  53.          this.displayable.processNormalProtocol();
  54.       } catch (Exception var2) {
  55.          ((Throwable)var2).printStackTrace();
  56.       }
  57.  
  58.       this.display.setCurrent(this.next);
  59.    }
  60.  
  61.    public void connectAndAuthenticate() throws IOException {
  62.       this.displayable.status("Connecting ...");
  63.       this.rfb = new rfbProto(this.host, this.port);
  64.       this.displayable.status("Reading supported version...");
  65.       this.rfb.readVersionMsg();
  66.       System.out.println("RFB server supports protocol version " + this.rfb.serverMajor + "." + this.rfb.serverMinor);
  67.       this.displayable.status("ServerMajor: " + this.rfb.serverMajor + " ServerMinor:" + this.rfb.serverMinor);
  68.       this.displayable.status("Writing own supported version ...");
  69.       this.rfb.writeVersionMsg();
  70.       this.displayable.status("Reading authentication scheme ...");
  71.       switch (this.rfb.readAuthScheme()) {
  72.          case 1:
  73.             System.out.println("No authentication needed");
  74.             this.displayable.status("No authentication needed");
  75.             break;
  76.          case 2:
  77.             this.displayable.status("Password authentication");
  78.             byte[] var1 = new byte[16];
  79.             this.rfb.is.readFully(var1);
  80.             String var2 = new String(this.password);
  81.             if (var2.length() > 8) {
  82.                var2 = var2.substring(0, 8);
  83.             }
  84.  
  85.             byte[] var3 = new byte[8];
  86.             byte[] var4 = var2.getBytes();
  87.  
  88.             for(int var5 = 0; var5 < var2.length(); ++var5) {
  89.                var3[var5] = var4[var5];
  90.             }
  91.  
  92.             for(int var6 = var2.length(); var6 < 8; ++var6) {
  93.                var3[var6] = 0;
  94.             }
  95.  
  96.             DesCipher var7 = new DesCipher(var3);
  97.             var7.encrypt(var1, 0, var1, 0);
  98.             var7.encrypt(var1, 8, var1, 8);
  99.             this.rfb.os.write(var1);
  100.             int var8 = this.rfb.is.readInt();
  101.             switch (var8) {
  102.                case 0:
  103.                   System.out.println("VNC authentication succeeded");
  104.                   break;
  105.                case 1:
  106.                   System.out.println("VNC authentication failed");
  107.                   break;
  108.                case 2:
  109.                   throw new IOException("VNC authentication failed - too many tries");
  110.                default:
  111.                   throw new IOException("Unknown VNC authentication result " + var8);
  112.             }
  113.       }
  114.  
  115.    }
  116.  
  117.    void doProtocolInitialisation() throws IOException {
  118.       System.out.println("sending client init");
  119.       this.displayable.status("Writing client init...");
  120.       this.rfb.writeClientInit();
  121.       this.displayable.status("Reading server init ...");
  122.       this.rfb.readServerInit();
  123.       System.out.println("Desktop name is " + this.rfb.desktopName);
  124.       System.out.println("Desktop size is " + this.rfb.framebufferWidth + " x " + this.rfb.framebufferHeight);
  125.       this.displayable.status("Setting encodings...");
  126.       this.setEncodings();
  127.    }
  128.  
  129.    void setEncodings() {
  130.       int[] var1 = new int[10];
  131.       int var2 = 0;
  132.       var1[var2++] = 5;
  133.       var1[var2++] = 0;
  134.       var1[var2++] = 2;
  135.       var1[var2++] = 4;
  136.  
  137.       try {
  138.          if (this.rfb != null && this.rfb.inNormalProtocol) {
  139.             this.rfb.writeSetEncodings(var1, var2);
  140.          }
  141.       } catch (Exception var4) {
  142.          ((Throwable)var4).printStackTrace();
  143.       }
  144.  
  145.    }
  146. }
  147.